Public Class CProductionStatus
    Inherits BOBase

    Protected m_State As ProductionStatusDS
    Protected m_DA As ProductionStatuss    
    Protected m_Trans As ArrayList
    Protected m_TransState As TransferDS

#Region "   Inherits Implementation"
    Sub New()
        m_State = New ProductionStatusDS
        m_DA = New ProductionStatuss        
        m_Trans = New ArrayList
        m_TransState = New TransferDS
    End Sub

    Public Overrides ReadOnly Property DA() As DABase
        Get
            If m_DA Is Nothing Then
                m_DA = New ProductionStatuss
            End If

            Return m_DA
        End Get
    End Property

    Public Overrides Property ObjectState() As DataSet
        Get
            Return m_State
        End Get
        Set(ByVal Value As DataSet)
            Try
                m_State = CType(Value, ProductionStatusDS)
            Catch ex As InvalidCastException
                Throw New ApplicationException("Invalid PRODUCTION_STATUS Dataset!", ex)
            End Try
        End Set
    End Property

    Public Overloads Overrides Sub Init()
        Throw New ApplicationException("Object doesn't support this method")
    End Sub

    Public Overloads Sub Init(ByVal orderItemID As Integer)
        Me.m_State.Clear()
        Dim dr As ProductionStatusDS.PRODUCTION_STATUSRow = m_State.PRODUCTION_STATUS.NewPRODUCTION_STATUSRow
        With dr
            .OI_ID_FK = orderItemID
            .LAST_UPDATE = Now
            '.QUANTITY_COMPLETED = -1 ' not set
            .SetQUANTITY_COMPLETEDNull()
            .SetQUANTITY_A50P_COMPLETEDNull()
            .SetQUANTITY_B50P_COMPLETEDNull()
            .SetEST_TIME_COMPLETEDNull()
            .SetPREV_EST_TIME_COMPLETEDNull()
            .SetCREATED_BYNull()
            .SetCREATED_DATENull()
            .SetUPDATED_BYNull()
            .SetADJUST_QUANTITYNull()
        End With
        m_State.PRODUCTION_STATUS.Rows.Add(dr)

    End Sub
#End Region

#Region "   Data Properties"
    Protected ReadOnly Property DataRow() As ProductionStatusDS.PRODUCTION_STATUSRow
        Get
            Return CType(Me.Row, ProductionStatusDS.PRODUCTION_STATUSRow)
        End Get
    End Property

    Public ReadOnly Property ID() As System.Int32
        Get
            Return Me.DataRow.PS_ID_PK
        End Get
    End Property

    Public Property OrderItemID() As System.Int32
        Get
            Return Me.DataRow.OI_ID_FK
        End Get
        Set(ByVal Value As System.Int32)
            Me.DataRow.OI_ID_FK = Value
        End Set
    End Property

    Public Property LastUpdated() As System.DateTime
        Get
            Return Me.DataRow.LAST_UPDATE
        End Get
        Set(ByVal Value As System.DateTime)
            Me.DataRow.LAST_UPDATE = Value
        End Set
    End Property

    Public Property QuantityCompleted() As System.Int32
        Get
            If Me.DataRow.IsQUANTITY_COMPLETEDNull Then
                Return Nothing
            Else
                Return Me.DataRow.QUANTITY_COMPLETED
            End If
        End Get
        Set(ByVal Value As System.Int32)
            Me.DataRow.QUANTITY_COMPLETED = Value
        End Set
    End Property

    Public Property QuantitypCompletedToNull() As System.Boolean
        Get
            Return Me.DataRow.IsQUANTITY_COMPLETEDNull()
        End Get
        Set(ByVal Value As System.Boolean)
            Me.DataRow.SetQUANTITY_COMPLETEDNull()
        End Set
    End Property

    Public Property QuantityA50pCompleted() As System.Int32
        Get
            If Me.DataRow.IsQUANTITY_A50P_COMPLETEDNull Then
                Return Nothing
            Else
                Return Me.DataRow.QUANTITY_A50P_COMPLETED
            End If
        End Get
        Set(ByVal Value As System.Int32)
            Me.DataRow.QUANTITY_A50P_COMPLETED = Value
        End Set
    End Property

    Public Property QuantityA50pCompletedToNull() As System.Boolean
        Get
            Return Me.DataRow.IsQUANTITY_A50P_COMPLETEDNull()
        End Get
        Set(ByVal Value As System.Boolean)
            Me.DataRow.SetQUANTITY_A50P_COMPLETEDNull()
        End Set
    End Property

    Public Property QuantityB50pCompleted() As System.Int32
        Get
            If Me.DataRow.IsQUANTITY_B50P_COMPLETEDNull Then
                Return Nothing
            Else
                Return Me.DataRow.QUANTITY_B50P_COMPLETED
            End If
        End Get
        Set(ByVal Value As System.Int32)
            Me.DataRow.QUANTITY_B50P_COMPLETED = Value
        End Set
    End Property

    Public Property QuantityB50pCompletedToNull() As System.Boolean
        Get
            Return Me.DataRow.IsQUANTITY_B50P_COMPLETEDNull
        End Get
        Set(ByVal Value As System.Boolean)
            Me.DataRow.SetQUANTITY_B50P_COMPLETEDNull()
        End Set
    End Property

    Public Property EstTimeCompleted() As System.DateTime
        Get
            If Me.DataRow.IsEST_TIME_COMPLETEDNull Then
                Return Nothing
            Else
                Return Me.DataRow.EST_TIME_COMPLETED
            End If
        End Get
        Set(ByVal Value As System.DateTime)
            If Value = Nothing Then
                Me.DataRow.SetEST_TIME_COMPLETEDNull()
            Else
                Me.DataRow.EST_TIME_COMPLETED = Value
            End If
        End Set
    End Property

    Public Property PrevEstTimeCompleted() As System.DateTime
        Get
            If Me.DataRow.IsPREV_EST_TIME_COMPLETEDNull Then
                Return Nothing
            Else
                Return Me.DataRow.PREV_EST_TIME_COMPLETED
            End If
        End Get
        Set(ByVal Value As System.DateTime)
            If Value = Nothing Then
                Me.DataRow.SetPREV_EST_TIME_COMPLETEDNull()
            Else
                Me.DataRow.PREV_EST_TIME_COMPLETED = Value
            End If
        End Set
    End Property

    Public Property CreatedBy() As System.Int32
        Get
            If Me.DataRow.IsCREATED_BYNull Then
                Return Nothing
            Else
                Return Me.DataRow.CREATED_BY
            End If
        End Get
        Set(ByVal Value As System.Int32)
            If Value = Nothing Then
                Me.DataRow.SetCREATED_BYNull()
            Else
                Me.DataRow.CREATED_BY = Value
            End If
        End Set
    End Property

    Public Property CreatedDate() As System.DateTime
        Get
            If Me.DataRow.IsCREATED_DATENull Then
                Return Nothing
            Else
                Return Me.DataRow.CREATED_DATE
            End If
        End Get
        Set(ByVal Value As System.DateTime)
            If Value = Nothing Then
                Me.DataRow.SetCREATED_DATENull()
            Else
                Me.DataRow.CREATED_DATE = Value
            End If
        End Set
    End Property

    Public Property UpdatedBy() As System.Int32
        Get
            If Me.DataRow.IsUPDATED_BYNull Then
                Return Nothing
            Else
                Return Me.DataRow.UPDATED_BY
            End If
        End Get
        Set(ByVal Value As System.Int32)
            If Value = Nothing Then
                Me.DataRow.SetUPDATED_BYNull()
            Else
                Me.DataRow.UPDATED_BY = Value
            End If
        End Set
    End Property

    Public Property AdjustQuantity() As Object
        Get
            If Me.DataRow.IsADJUST_QUANTITYNull Then
                Return Nothing
            Else
                Return Me.DataRow.ADJUST_QUANTITY
            End If
        End Get
        Set(ByVal Value As Object)
            If Value = Nothing Then
                Me.DataRow.SetADJUST_QUANTITYNull()
            Else
                Me.DataRow.ADJUST_QUANTITY = Value
            End If
        End Set
    End Property

    Public ReadOnly Property Week() As Integer
        Get
            Return Common.Week(Me.LastUpdated)
        End Get
    End Property

    Public ReadOnly Property Year() As Integer
        Get
            Return Common.Year(Me.LastUpdated)
        End Get
    End Property
#End Region

#Region "   Transfer properties"
    Public ReadOnly Property Transfer(ByVal index As Integer) As CTransfer
        Get
            Return CType(Me.m_Trans(index), CTransfer)
        End Get
    End Property

    Public ReadOnly Property Transfers() As ArrayList
        Get
            Return Me.m_Trans
        End Get
    End Property
#End Region

    Private Sub BuildTransferCollection()
        Me.m_Trans.Clear()
        Dim i As Integer
        For i = 0 To Me.m_TransState.TRANSFER.Rows.Count - 1
            If Me.m_TransState.TRANSFER(i).RowState <> DataRowState.Deleted Then
                Me.m_Trans.Add(New CTransfer(Me.m_TransState, i))
            End If
        Next
    End Sub

#Region "   Overrides"
    Public Overloads Overrides Sub Load(ByVal keys() As Object)
        Throw New ApplicationException("Object doesn't support this method")
    End Sub

    Public Overloads Sub Load(ByVal orderID As Object, ByVal week As Integer, ByVal year As Integer)
        Dim keys(2) As Object
        keys(0) = orderID
        keys(1) = week
        keys(2) = year

        MyBase.Load(keys)

        Dim daTrans As New Transfers
        daTrans.SelectByProductionStatusID(Me.ID, Me.m_TransState, m_TransState.TRANSFER.TableName)

        BuildTransferCollection()
    End Sub

    Public Overloads Overrides Sub Save()
        Me.DA.BeginTrans()
        Try
            MyBase.Save()

            Dim i As Integer
            For i = 0 To Me.m_Trans.Count - 1
                If Me.Transfer(i).Row.RowState <> DataRowState.Deleted Then
                    ' assign ID for non-deleted row
                    Me.Transfer(i).ProductionStatusID = Me.ID
                End If
            Next

            If Me.Transfers.Count > 0 Then
                ' save child table, only need to save the first transfer item
                ' because "Save" in DABase mean Update a whole dataset
                Me.Transfer(0).Save(CType(Me.DA.Transaction, SqlClient.SqlTransaction))
            End If

            Me.DA.CommitTrans()
        Catch ex As Exception
            Me.DA.RollBackTrans()
            Throw ex
        End Try

    End Sub

    Public Overloads Overrides Sub Save(ByVal sqlTrans As SqlTransaction)
        MyBase.Save(sqlTrans)

        Dim i As Integer
        For i = 0 To Me.m_Trans.Count - 1
            If Me.Transfer(i).Row.RowState <> DataRowState.Deleted Then
                ' assign ID for non-deleted row
                Me.Transfer(i).ProductionStatusID = Me.ID
            End If
        Next

        If Me.Transfers.Count > 0 Then
            ' save child table, only need to save the first transfer item
            ' because "Save" in DABase mean Update a whole dataset
            Me.Transfer(0).Save(sqlTrans)
        End If

    End Sub

    Public Overloads Overrides Sub Remove()
        MyBase.Remove()

        'doesn't need to remove child table rows, 'cause we were enabled cascade delete in DB
        Me.m_Trans.Clear()
        Me.m_TransState.Clear()
    End Sub

#End Region

    Public Function AddNewTransfer(ByVal POId As Integer) As Integer
        Dim row As TransferDS.TRANSFERRow = Me.m_TransState.TRANSFER.NewTRANSFERRow
        With row
            .PS_ID_FK = Me.ID
            .PO_ID_FK = POId
            .QUANTITY = -1  ' mean: not set
        End With
        Me.m_TransState.TRANSFER.AddTRANSFERRow(row)

        Me.m_Trans.Add(New CTransfer(Me.m_TransState, m_Trans.Count))
        Return m_Trans.Count - 1
    End Function

    Public Function LastWeek() As CProductionStatus
        Dim objPS As New CProductionStatus
        Dim week As Integer = Me.Week
        Dim year As Integer = Me.Year

        If week - 1 <= 0 Then
            year -= 1
            week = 52
        Else
            week -= 1
        End If

        objPS.Load(Me.OrderItemID, week, year)

        Return objPS
    End Function

    Public Shared Function GetProductionStatusWeeklyList(ByVal customerID As Integer) As DataSet
        Dim ds As DataSet
        ds = DataHelper.ExecuteDataSet2("GetProductionStatusWeeklyList", customerID)
        Return ds
    End Function

End Class
